<?php

defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'detail';
if($_RQ['op']=='detail'){
	/*******main*******/
	$return['ischecklogin']=$this->model('member')->checklogin();
	if(intval($_RQ['fromuser'])){
		$this->model('member')->setfromuser();
	}
	$id = intval($_RQ['id']);
	if(!$id){
		exi('该商品不存在!','error');
	}
	$this->session('goodsid',$id);
	$team=pdo_get('jy_guashop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	if($team && $team['status']==0){
		$this->model('model')->add_yao($team);
	}
	$day=date('Y/m/d');
	$hourkey=$this->model('model')->gethourkey();
	$team['isnum']=pdo_count('jy_guashop_canlog',array('goodsid'=>$team['id'],'uniacid'=>$_SESSION['uniacid']),'id');
	if(!$team['goodstype']){
		$goods=pdo_get('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$team['goodsid']));
		$goods['param']=json_decode($goods['param'],true);
		$goods['images']=json_decode($goods['images'],true);
		$team['image']=$goods['image'];
		$team['oldprice']=$goods['price'];
	}else{
		$team['oldprice']=$team['price']*2;
	}
	$team['createtime']=date('Y/m/d H:i:s',$team['createtime']);
	$team['salenum']=pdo_count('jy_guashop_canlog',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$team['id']),'id');
	$team['statustitle']=$this->goods_status[$team['status']];
	$log=pdo_get('jy_creditshop_log',array('uniacid'=>$_SESSION['uniacid'],'id'=>$team['logid']));
	$team['goods']=pdo_get('jy_creditshop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$log['goodsid']),array('title','thumb','goodsdetail'));
	$return['team']=$team;
	$return['goods']=$goods;
	$return['list']=array();
	if(!$goods['type']){
		$sendtype=1;
	}else{
		$sendtype=3;
	}
	$address=pdo_get('core_address',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'','isdefault desc');
	$address['pcd']=json_decode($address['pcd'],true);
	$return['post']=array('goodsid'=>$team['id'],'sendtype'=>$sendtype,'address'=>$address);
	$return['params']=array('status'=>0,'goodsid'=>$team['id']);
	$return['pluginset']['jian_feetype']=$return['pluginset']['jian_feetype']?:3;
	$return['pluginset']['jian_feetypetitle']=$this->credit_type[$return['pluginset']['jian_feetype']];
	$return['sharedata']=array(
			'title'=>$nickname.'-发起挂卖活动啦！参与就可获利了！',
			'desc'=>'参与挂卖活动啦，参与就可获利了！',
			'link'=>custom_url('jy_guashop/detail//id='.$team['id'].'&fromuser='.$_SESSION['uid'],true),
			'imgUrl'=>tomedia($team['image'])
	);
	if($this->setting['tpl_type']){
		//$return['htmls']=$this->template_url('detail1');
	}
	$return['btitle']=$this->btitle;
	set_html('title','挂卖详情');
	exi($return);	
}
if($_RQ['op']=='getlist'){
	$team=pdo_get('jy_guashop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$this->session('goodsid')));
	$pindex = max(1, intval($_RQ['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$this->session('goodsid'));
	if($_RQ['status']==3){
		$params['status']=array(3,4,6,7);
	}elseif($_RQ['status']==1){
		$params['status']=array(2,5);
	}else{
		$params['status']=array(0,1);
	}
    $orderby='id desc';
	$total='';
	$return['list']=pdo_getslice('jy_guashop_canlog',$params,array($pindex,$psize),$total,$fields,'',$orderby);	
	foreach($return['list'] as &$v){
		$v['statustitle']=$this->canlog_status[$v['status']];
		$v['createtime']=date('Y-m-d H:i:s',$v['createtime']);
		$v['daytitle']=$v['dayid'].' '.$this->hourkey[$v['hourkey']];
		$v['member']=pdo_get('core_members',array('id'=>$v['mid']),array('nickname','avatar'));
		$v['member']['nickname']=$v['member']['nickname']?cfc('tools')->substr($v['member']['nickname'],0,1).'****':'******';
	}
	exi($return);
}
// 完成下单
if($_RQ['op']=='post'){
	$post=get_postdata();
	$goodsid=$this->session('goodsid');
	$team=pdo_get('jy_guashop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$goodsid));
	$member=model('core/member')->detail($_SESSION['uid'],array('credit1','credit2','credit3','credit4','credit6','mobile','avatar','nickname'));
	$this->model('model')->check_buy($team,$post,true,$member);
	$log=array(
				'goodsid'=>$goodsid,
				'fee'=>$team['price'],
				'sendtype'=>$post['sendtype'],
				'address'=>ijson_encode($post['address']),
    );
	$order_id=$this->model('model')->up_canlog($log);
	$params=array(
			'id'=>$order_id,
			'title'=>'商品挂卖',
			'fee'=>$team['price'],
			'back_url'=>'jy_guashop/detail/payresult/',
			'success_url'=>'jy_guashop/canlog/',
			'error_url'=>'jy_guashop/canlog/',
    );
	$payid=cfc('pay')->create_pay_order($params);
	$this->pdo_update('canlog',array('pay_id'=>$payid),array('id'=>$order_id));
	$res=array(
			'pay_id'=>$payid,
			'order_id'=>$order_id,
			'success_url'=>'jy_guashop/canlog/',
			'error_url'=>'jy_guashop/canlog/',
    );
	exi($res);
}
if($_RQ['op']=='payresult'){
	$payresult=$_SESSION['pay_result'];
	if($payresult['status']!=1 && $payresult['pay_type'] !='hdfk'){
		exi('支付失败。','error','jy_guashop/canlog/');
	}
	$order=pdo_get('jy_guashop_canlog',array('id'=>$payresult['id']));
	if($order['ispay'] ==1){
		exi('该订单已经支付！','error','jy_guashop/canlog/');
	}
	if($order['fee']!=$payresult['fee']){
		exi('支付失败！订单金额不匹配！'.$order['price'].'|'.$payresult['fee'],'error','jy_guashop/canlog/');
	}
	$params=array(
		'paytype'=>$payresult['pay_type'],
		'transid'=>$payresult['transaction_id'],
	);
	$this->model('model')->canlog_pay($order,$params);
	exi('提交成功!');
}
?>